﻿using BrnMall.Manager.Domain.Blog;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static BrnMall.Manager.Domain.User.User;

namespace BrnMall.Manager.Manager.Blog
{
   public static class BlogManager
    {
        static string TableName = "crm_blog";
        /// <summary>
        /// 获取动态帖子列表
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="staffId"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static Page<Blogs> GetBlogList(int storeId, int uid, int page, int pageSize = 10)
        {
            var sql = string.Empty;
            if (uid > 0)
                sql = string.Format("select * from crm_blog where storeid={0} and Uid={1} order by id desc", storeId, uid);
            else
                sql = string.Format("select * from crm_blog where storeid={0} order by id desc", storeId);

            using (var db = new QDMallDatabase())
            {
                return db.Page<Blogs>(page, pageSize, sql);
            }
        }

        public static Blogs Get(int id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<Blogs>(string.Format("select * from {0} where id={1}", TableName, id));
            }
        }

        /// <summary>
        /// 根据id得到用户信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public static List<UsersModel> GetByIds(string uid)
        {
            var sql = string.Empty;
            if (uid.Contains(","))
                sql = string.Format("select uid,nickname,avatar,mobile from bma_users where uid in({0})", uid);
            else
                sql = string.Format("select uid,nickname,avatar,mobile from bma_users where uid={0}", uid);

            using (var db = new QDMallDatabase())
            {
                return db.Fetch<UsersModel>(sql);
            }
        }
        /// <summary>
        /// 发布帖子
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool InsertBlogs(Blogs Model)
        {
            object obj = null;
            using (var db = new QDMallDatabase())
            {
                obj = db.Insert(Model);
                if (obj != null)
                    return true;
                return false;
            }
        }


        /// <summary>
        /// 删除动态贴
        /// </summary>
        /// <param name="Msgid"></param>
        /// <returns></returns>
        public static bool DeleteBlogById(int storeid, int id)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute("delete crm_blog where storeid=@0 and id=@1", storeid, id);
            }
            return count > 0 ? true : false;
        }
        
        /// <summary>
        /// 添加浏览数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool AddClick(int id)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count = db.Execute(string.Format("update crm_blog set ClickCount=isnull(ClickCount,0)+1 where id={0}", id));
            }
            return count > 0 ? true : false;
        }

        /// <summary>
        /// 添加点赞
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type">类型(1 加点赞数,2 取消点赞数)</param>
        /// <returns></returns>
        public static bool AddUp(int id, int type)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                if (type == 2)
                    count = db.Execute(string.Format("update crm_blog set UpCount=UpCount-1 where id={0} and UpCount>0", id));  //减点赞数
                else
                    count = db.Execute(string.Format("update crm_blog set UpCount=isnull(UpCount,0)+1 where id={0}", id));  //加点赞数
            }
            return count > 0 ? true : false;
        }
    }
}
